<?php
/**
 * beimuaihui System
 * Copyright(c) 2011-2020 beimuaihui.
 * @license    http://www.gnu.org/licenses/gpl.html     This software Under GPL V3 License 
 * beimuaihui@gmail.com
 * http://code.google.com/p/beimuaihui/
 * $Id: ModelrelationController.php 207 2011-03-03 13:11:36Z beimuaihui $
 */



class System_RelationinmodelController extends Baogg_Controller_Action {

	function init() {
		$this->ModelRelation = new ModelRelation ( );
		$this->RelationInModel = new RelationInModel ( );
		$this->model_id=$this->getRequest()->getParam('model_id');
		
		//echo '<pre>';print_r($this->model_id);exit;
	}

	/**
	 * The default action
	 */
	public function listAction() {
		global $LANG;
		/* var_dump($this->getRequest()->get('model_id'));
		var_dump($this->getRequest()->getPost('model_id'));
		exit; */
		
		$params=$this->getRequest()->getParams();
		$model_id=@$params['model_id'];
		$form_url=$params;
		BAOGG_LANGUAGE::loadFile("model");
		BAOGG_LANGUAGE::loadFile("model_relation");
		
		
		Zend_Loader::loadClass("Baogg_View_Extgrid");
		
		$cm=array();
		$cm['relation_in_model_id']=array("hidden"=>true,"cmType"=>Baogg_View_Extgrid::CMTYPE_PK) ;
		$cm['model_relation_id']=array("hidden"=>true);
		$cm['model_relation_name']=array("sortable"=>true);
		//$cm['model_name']=array("sortable"=>true);
		$cm['model_id']=array("hidden"=>true);
		$cm['model_name']=array("sortable"=>true);		
		$cm['p_model_alias']=array("sortable"=>true);		
		$cm['filter']=array("hidden"=>true);		
		$cm['sorting']=array("hidden"=>true);		
		$cm['is_select']=array("hidden"=>true);
		$cm['is_update']=array("hidden"=>true);
		$cm['is_delete']=array("hidden"=>true);
		$cm['state']=array("hidden"=>true);
		$cm['ordering']=array("hidden"=>true);
		//$cm['sub_table']=array("hidden"=>true,"cmType"=>Baogg_View_Extgrid::CMTYPE_SUBTABLE) ; //Baogg_View_Extgrid::;
		
		$form_url['action']="data";
		$grid=new Baogg_View_Extgrid($form_url, $cm);
		$grid->setSelectionModel(true);
		$grid->setRowNumberer(true);
		$grid->addExistButtons(array('add','remove','query'),array('edit','delete'));

		$ModelRelation=new ModelRelation();
		$combo_relation=$ModelRelation->getCombo(null,null,null,array('model_relation_id','model_relation_name'));
		
		
		/* 
	    $rs_pid=$Model->getList();
	    $combo_pid=array();
	    foreach((array)$rs_pid as $v){
	    	$combo_pid[]=array($v['model_id'],$v['model_name']);
	    } */
	    
	    $Model=new Model();
	    $rs_model_name=$Model->getOne($this->model_id);
	    if($rs_model_name){
	    	$model_name=$rs_model_name[0]['model_name'];
	    }else{
	    	throw new Exception('not exist model id');
	    }

        
		$this->view->LANG=Zend_Json::encode($LANG);
		$this->view->grid = $grid->render();
		$this->view->model_id=$this->model_id;
		
			
		$form_url['action']='add';
		$form=new Baogg_View_Extform(array("url"=>$form_url,"height"=>390,"width"=>600));
		$form->addItem(0,array('name'=>'model_name','xtype'=>'displayfield','value'=>$rs_model_name[0]['model_name']));
		
		$form->addItem(0,array('name'=>'model_relation_id','xtype'=>'combo','store'=>$combo_relation));
		$form->addItem(0,array('name'=>'p_model_alias'));
		
		$form->addItem(0,array('name'=>'filter'));		
		$form->addItem(1,array('name'=>'sorting'));
		$form->addItem(1,array('name'=>'is_select','xtype'=>'radiogroup','source'=>BAOGG_LANGUAGE::array2store("arr_state")));
		$form->addItem(1,array('name'=>'is_update','xtype'=>'radiogroup','source'=>BAOGG_LANGUAGE::array2store("arr_state")));
		$form->addItem(1,array('name'=>'is_delete','xtype'=>'radiogroup','source'=>BAOGG_LANGUAGE::array2store("arr_state")));
		
		$form->addItem(2,array('name'=>'model_id',"xtype"=>"hidden","hideLabel"=>true,'value'=>$this->model_id));	
		$form->addItem(2,array('name'=>'relation_in_model_id',"xtype"=>"hidden","hideLabel"=>true));	
		$form->addExistButtons(array('-','save_grid','save_new','save','-','search','-','cancel'));//,'config','config'
		$this->view->form=$form;


	}

	/**
	 * The data action
	 */
	function dataAction() {		
	    $post=$this->_request->getPost ();
	    $model_id=$this->_request->getParam('model_id');
	    
	    $this->RelationInModel->autoloadRelation($model_id);
	    
	    $where=array();$order=array();$limit=array();
	   foreach((array)$post as $k=>$v){
	       if($k=="start" || $k=="limit"){
	           $limit[$k]=$v;
	       }else if($k=="sort" || $k=="dir"){
	           $order[$k]=$v;
	       }else{
	           if($k=="model_id"  ){
	               if($v && is_numeric($v)){
	                   $v=(int)$v;
	                   $where[$k]=$v;
	               }
	           }else{
	               $v='%'.$v.'%';
	               $where[$k]=$v;
	           }
	           
	       }
	   }
	   if(!$order){
	   		$order=array("sort"=>"ordering","dir"=>"asc");
	   }
	   if($model_id){
	   	$where['model_id']=(int)$model_id;
	   }
	   $RelationInModel=new RelationInModel();
	   $data=$RelationInModel->getList($where,$order,$limit,array('*'),array(
	   		array("name"=>"model","condition"=>'relation_in_model.model_id=model.model_id','cols'=>array('model_name')),
	   		array("name"=>"model_relation","condition"=>'relation_in_model.model_relation_id=model_relation.model_relation_id','cols'=>array('model_relation_name'))
	   		));
	   $rs_count=$RelationInModel->getList($where,null,null,array('count(*) as cnt'),array(
	   		array("name"=>"model","condition"=>'relation_in_model.model_id=model.model_id','cols'=>array('model_name')),
	   		array("name"=>"model_relation","condition"=>'relation_in_model.model_relation_id=model_relation.model_relation_id','cols'=>array('model_relation_name'))
	   		));
	   
	   //echo Baogg_Db::debugDb(Zend_Registry::get('db'));exit;
	   $this->view->grid = Zend_Json::encode(array("count"=>@$rs_count[0]['cnt'],"data"=>$data));
	}

	
	/**
	 * The add action
	 */
	function addAction(){		
		$this->saveAction();		
	}
	
	/**
	 * The edit action
	 */
	function editAction() {
		$this->saveAction();
	}

	/**
	 * The delete action
	 */
	function deleteAction() {
        
	    $ret=array("success"=>false,"msg"=>"delete_fail");
	    $id = Baogg_Controller_Action::_getParam ( 'ids' );
		 
		   
		
		if ($this->RelationInModel->_delete ( $id )){
			$ret['success'] = true;
			$ret["msg"]="delete_ok";
		}
		
		echo Baogg_Language::outputResult($ret);
		 $this->_helper->viewRenderer->setNoRender ();
	}
	
	function removeAction(){
		$this->deleteAction();
	}

	/**
	 * The save action
	 */
	function saveAction() {
		//global $LANG;
		
	    $ret=array("success"=>false,"msg"=>"save_fail");
	   
	    // echo '<pre>';print_r($this->ModelRelation->info(ModelRelation::METADATA));exit;
	    //check form
		if (!$this->_request->isPost ())
		{
		   echo Baogg_Language::outputResult($ret);
		   exit;
		}
		
		$formData = $this->_request->getPost ();

		if(!$formData)
		{
		   echo Baogg_Language::outputResult($ret);
		   exit;     
		}
			
		$arr=$this->RelationInModel->filterForm($formData);
        $action=$this->getRequest()->getParam('action');
		$rs = $this->RelationInModel->getSame(array('relation_in_model_id'=>$arr["relation_in_model_id"]));
		if($action=='edit'){
			//update record
			//$permission_type = 2;
			//echo '<pre>';print_r($rs);var_dump(count($rs));exit;
			if(count($rs)==1 &&  $rs[0] == $arr["relation_in_model_id"] ){
			    $this->RelationInModel->_update($arr,$arr["relation_in_model_id"]);
			    $Model=new Model();
			    if($arr["is_select"]==1){
			    	$this->RelationInModel->copyField($arr["relation_in_model_id"]);
			    }
			    $ret['success'] = true;				
				$ret['msg'] = "save_ok";			
			}else{
				$ret['msg'] = "exist_same_record";
			}
			
		}else{
			//insert record
			//$permission_type = 1;			
			if($rs){				
				$ret['msg'] = "exist_same_record";
			}else{
			    unset($arr["relation_in_model_id"]);
				$ret['success'] =$relation_in_model_id= $this->RelationInModel->_insert($arr);
				if($ret['success']){
					if($arr["is_select"]==1){
				    	$this->RelationInModel->copyField($relation_in_model_id);
				    }
				}
				$ret['msg'] = "add_ok";
			}
		}

		$model=new Model();
		
		$ret['success']=$ret['success'] && $model->genExtTable($formData['model_id']);
		//$db=Zend_Registry::get("db");
		//echo Baogg_Db::debugDb($db);exit;
		echo Baogg_Language::outputResult($ret);
		
		$this->_helper->viewRenderer->setNoRender ();
		
		
	}

}
